package com.drondea.testclient.handler.sgip.server;

import com.drondea.sms.channel.ChannelSession;
import com.drondea.sms.message.IMessage;
import com.drondea.sms.type.ICustomHandler;
import com.drondea.sms.type.UserChannelConfig;
import com.drondea.sms.windowing.DuplicateKeyException;
import com.drondea.sms.windowing.OfferTimeoutException;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;


/**
 * @version V3.0.0
 * @description sgip的定制处理器
 * @author liyuehai
 * @date 2020年06月23日17:35
 **/

@Slf4j
public class SgipServerCustomHandler extends ICustomHandler {
    
    private static final int DELAY_RETRY_COUNT = 30;
    private static final int LOG_RETRY_COUNT = 20;

    @Override
    public void fireUserLogin(Channel channel, ChannelSession channelSession) {
        log.debug("客户端登录了");
    }

    @Override
    public void channelClosed(ChannelSession channelSession) {

    }

    @Override
    public void configPipelineAfterLogin(ChannelPipeline pipeline) {
        pipeline.addLast("ServerSgipReportRequestHandler", new ServerSgipReportRequestHandler());
        pipeline.addLast("NettyTailHandler", new TailHandler());
    }

    @Override
    public void responseMessageExpired(Integer sequenceId, IMessage request) {
        System.out.println("短信超时处理" + sequenceId);
    }

    @Override
    public void slidingWindowException(ChannelSession session, ChannelHandlerContext ctx, IMessage message, ChannelPromise promise, Exception exception) {
        log.error("slidingWindowException", exception);
        int retryCount = message.addRetryCount();
        // 失败越多延时越长，防止线程堆积
        int delay = 10;
        if (retryCount >= DELAY_RETRY_COUNT) {
            delay = 500 * retryCount / 10;
        }
        if (retryCount > LOG_RETRY_COUNT) {
            log.info("当前重试次数：{}", retryCount);
        }
        // 重写
        ctx.executor().schedule(() -> session.sendWindowMessage(ctx, message, promise), delay, TimeUnit.MILLISECONDS);
        // 滑动窗口key冲突
        if (exception instanceof DuplicateKeyException) {
            return;
        }
        // 滑动窗口获取slot超时
        if (exception instanceof OfferTimeoutException) {
            return;
        }
        if (exception instanceof InterruptedException) {
            return;
        }
    }

    @Override
    public boolean customLoginValidate(IMessage message, UserChannelConfig channelConfig, Channel channel) {
        return true;
    }

    @Override
    public void failedLogin(ChannelSession channelSession, IMessage msg, long status) {

    }

}
